<br />
<br />
<p style="color: red; font-weight: bold;font-size: 30px;margin-left:180px ;">THÔNG TIN LỊCH COI THI</p><br />
<p style="margin-left:200px;">Lọc danh sách : 
    <?php
	
        $sort=$_GET['sort'];
        $p=$_GET['page'];
       
        $sqlcount="Select count(*) from `lichthi` lt,`cathi` ct,`phongthi` pt where
			lt.`idphong`=pt.`idphong` and ct.`idca`=lt.`idca`";
        $rscount=mysql_query($sqlcount);
        $r=mysql_fetch_row($rscount);
        
         $sql="Select * from `lichthi` lt,`cathi` ct,`phongthi` pt where
			lt.`idphong`=pt.`idphong` and ct.`idca`=lt.`idca`";
          
        $p=max(0,round($p));
       
        if(!isset($_GET['page']))
			{
				$_GET['page'] = 1;
			}
        $ipp=10;
        $noi=$r[0];
        
        $nop=ceil($noi/$ipp);
        
        $p=min($nop,$p);	
        $pos=($p-1)*$ipp;
       
        if($sort==1){
            $sql.="and `anhien`=1 Order by lt.`thoigian` desc";
        }
        if($sort==2){
            $sql.="and `anhien`=1 Order by lt.`thoigian` asc";
        }
        if($sort==3){
            $sql.="and `anhien`=1 Order by ct.`idca` asc";
        }
        if($sort==4){
            $sql.="and `anhien`=1 and lt.`songuoidadangky` < lt.`soluonggiamthi`";
        }
       
        if($p>=1){
           
            $sql.=" LIMIT $pos,$ipp";
         
        }
        
        
     ?>
    <select onchange="window.location='?mod=dangkylichcoithi&sort='+this.value+'&page=<?php echo $p?>'">
	<option <?php if($sort==0)echo 'selected'; ?> value="0">---Lựa chọn kiểu lọc---</option>
	<option <?php if($sort==1)echo 'selected'; ?> value="1">Lọc theo lịch thi mới </option>
    <option <?php if($sort==2)echo 'selected'; ?> value="2">Lọc theo lịch thi cũ</option>
    <option <?php if($sort==3)echo 'selected'; ?> value="3">Lọc theo ca thi</option>
    <option <?php if($sort==4)echo 'selected'; ?> value="4">Lọc lịch trống</option>

</select></p><br />
<form action="" method="post">
<table border="1"  style="width: 690px;" >
 <!-- begin caption -->
	<tr height="30">
    	<th>Đăng ký</th>
        <th>Ngày thi</th>
        <th>Phòng <br />
thi</th>
        <th>Ca thi</th>
        <th>Thời gian <br />
thi</th>
        <th>Số lượng<br />
 người đăng ký
</th>	
    <th>Hình thức<br />
 thi </th>
    <th>Số lượng <br />
thí sinh</th>
    <th>Ghi chú</th>  
    <tr>
    <!-- end caption -->
   
 <?php
		$rs=mysql_query($sql);
		$count=0;
		while($r=mysql_fetch_assoc($rs)){
		$count++;
	 ?>
    <tr style="background-color: <?php if($count%2!=0) echo "#CCC";  ?>">
    	<td align="center"><label><input <?php 
        // kiểm tra xem la sv đó đã đăng ký lịch này chưa bằng so khớp stt
        // của lịch thi và kq dang ky
        // nếu đang ky rồi check box sẽ chuyển thành dấu check đồng thời
        // sẽ disable 
        $sqlkqdk="SELECT * FROM `ketquadangky`";
        $rskqdk=mysql_query($sqlkqdk);
        while($rkqdk=mysql_fetch_assoc($rskqdk)){
            if($rkqdk['stt']==$r['stt'] && $rkqdk['mssv']==$_SESSION['iduser']){
                echo "disabled='disabled'";
                echo "checked='check'";
            }
        }
        
        if($r['songuoidadangky']==$r['soluonggiamthi']) {echo "disabled='disabled'";}
        
        ?> type="checkbox" name="stt[]" value="<?php echo $r['stt'] ?>" /></label></td>
        <td align="center"><?php echo convertDateVN($r['thoigian']);  ?></td>
        <td align="center"><?php echo $r['tenphong'] ?></td>
        
        <td align="center"><?php echo $r['tenca'] ?></td>
        <td align="center"><?php echo $r['gio'] ?></td>
        <td align="center"><?php echo "<span style='color:red'>".$r['songuoidadangky']."</span>"."/".$r['soluonggiamthi'] ?></td>
        <td align="center"><?php 
			foreach($array as $k=>$v){
				if($k==$r['hinhthucthi']){
					echo $v;
					}
				}
		?></td>
        <td align="center"><?php echo $r['sothisinh'] ?></td>
        <td><?php echo $r['ghichu'] ?></td>
    	
    </tr>
 <?php } ?>
</table><br />
<br />
    <!-- Insert dữ liệu vào kết quả đăng ký đồng thời cũng
        update dữ liệu so nguoi dang ky gac thi o bang lich thi
     -->
	<?php
    if(isset($_POST['btnDangky'])){
		if(isset($_POST['stt'])){
			foreach($_POST['stt'] as $k=>$v){
					$sql2="SELECT * FROM `lichthi` where `stt`=".$v;
					$rs2=mysql_query($sql2);
					$r2=mysql_fetch_assoc($rs2);
					$mssv=$_SESSION['iduser'];
                    $dt= new DateTime('now', new DateTimeZone('Asia/Ho_Chi_Minh'));
                    $currentTime=$dt->format('Y-m-d H:i:s');
					$sql3="INSERT INTO `ketquadangky` VALUES ($v,$mssv,'$currentTime','')";
					$sql4="Update `lichthi` set `songuoidadangky`=`songuoidadangky`+1 where `stt`=".$v;
                    mysql_query($sql4);
                    mysql_query($sql3);
					
				}
                
                 header('location:?mod=ketquadangky');
                
			     
            }
            
             
        }	
	 ?>
     <?php
		 
           
			echo '<div id="center2">';
			echo '<ul id="pagination-digg">';
			if($p > 1)
			{
				echo '<li class="previous"><a href="?mod=dangkylichcoithi&sort='.$sort.'&page='.($p-1).'">Back</a></li>';
			}
			for($i=1;$i<$nop;$i++)
			{
				if($p == $i)
				{
					echo '<li class="active">'.$i.'</li>';
				}
				else
				{
					echo '<li><a href="?mod=dangkylichcoithi&sort='.$sort.'&page='.$i.'">'.$i.'</a></li>';
				}
			}
			if($p < $nop)
			{
				echo '<li class="next"><a href="?mod=dangkylichcoithi&sort='.$sort.'&page='.($p+1).'">next</a></li></ul></div>';
			}
			else
			{
				echo '<li class="next-off">next</li></ul></div>';
			}
		 ?>        
    
	<input type="submit" value="Đăng ký" name="btnDangky" style="height:40px; width:70px;margin-left:300px" />
</form>